// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
Position.center = function(element){
  var options = Object.extend({
      zIndex: 5000,
      update: false
  }, arguments[1] || {});

  element = $(element)

  if(!element._centered){
      Element.setStyle(element, {position: 'absolute', zIndex: options.zIndex });
      element._centered = true;
  }

  var dims = Element.getDimensions(element);

  Position.prepare();
  //var winSize = Position.getWindowSize();
  var winSize = document.viewport.getDimensions();
  var winWidth = winSize.width;
  var winHeight = winSize.height;

  var offLeft = (Position.deltaX + Math.floor((winWidth-dims.width)/2));
  var offTop = (Position.deltaY + Math.floor((winHeight-dims.height)/2));
  element.style.top = ((offTop != null && offTop > 0) ? offTop : '0')+ 'px';
  element.style.left = ((offLeft != null && offLeft > 0) ? offLeft :'0') + 'px';

  if (options.update) {
    Event.observe(window, 'resize', function(evt) { Position.center(element); }, false);
    Event.observe(window, 'scroll', function(evt) { Position.center(element); }, false);
  }
};

var forms = ['order_form_mortgage', 'order_form_auto', 'order_form_bail'];

function show_form(id)
{
	var element = $(id);	
	
	for(var i = 0; i < forms.length; i++)
	{
		if (forms[i] != null && forms[i] != id)
			$(forms[i]).hide();
	}
	
	Position.center(element);
	
	element.toggle();
	Preloader.show();	
	
	/*if (element.style.display == '')
	{
		//element.fade();
		new Effect.BlindUp(element)
	}
	else
	{
		new Effect.BlindDown(element)
	}*/
}

function show_question(element)
{
	element = $(element);
	var question = $('question');
	$('calc_form').hide();
	//var pos = Position.page(element);
	var pos = element.cumulativeOffset();
	
	question.style.left = (pos.left - 260) + 'px';
	question.style.top = (pos.top - 230) + 'px';
	
	Position.center(question);
	question.toggle();
	Preloader.show();
}

function show_calc(element)
{
	element = $(element);
	var question = $('calc_form');
	$('question').hide();
	//var pos = Position.page(element);
	var pos = element.cumulativeOffset();
	
	question.style.left = (pos.left - 200) + 'px';
	question.style.top = (pos.top - 330) + 'px';
	
	Position.center(question);
	question.toggle();
	Preloader.show();
}

var last_menu = null;
var time_show = 2000;

function show_menu(element)
{
	element = $(element)
	var id = element.id.replace('menu_', 'sub_menu_');
	var menu = $(id);
	
	if (menu)
	{
		var pos = element.cumulativeOffset();
		menu.style.left = pos.left + "px";
		menu.style.top = (pos.top + 20) + "px";
		
		var links = menu.select('a')
		for(var i = 0; i < links.length; i++)
		{
			Event.observe(links[i], "mouseover", stopTimeout);
			Event.observe(links[i], "mouseout", startTimeout);
		}
		
		Event.observe(element, "mouseout", startTimeout);
		menu.show();
		last_menu = menu.id;
	}
}

function hide_menu()
{
	if (last_menu) $(last_menu).hide();
}
 
function stopTimeout()
{
	if (document.body.getAttribute("hide_menu_time_out") !=null)
	{
		ClearTimeOut("hide_menu_time_out");
	}
}

function startTimeout()
{
	ClearTimeOut("hide_menu_time_out");
	document.body.setAttribute("hide_menu_time_out", window.setTimeout(hide_menu, time_show));
}

function ClearTimeOut(oAttribute)
{
	var id=document.body.getAttribute(oAttribute);
	if (id !=null)
	{
		window.clearTimeout(id);
	}
	document.body.removeAttribute(oAttribute);
}

var Preloader = {
	show: function(target, element)
	{
		element = element == null ? 'preloader' : element;
		this.element = $(element);
		if (this.element == null) return;
		target = (target == null) ? window.document : $(target);
		//var pos = target.cumulativeOffset();
		var viewport = target.viewport.getDimensions();
		var height = viewport.height / 2;
		var img = this.element.down('img');
		if (img) img.style.marginTop = (height - 5) + 'px';
		
		var client = window.innerHeight || document.documentElement.clientHeight;
    var full = document.documentElement.scrollHeight;
    var top = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
		
		this.element.style.left = "0px";
		this.element.style.top = "0px";
		this.element.style.width = viewport.width + 'px';
		this.element.style.height = full + 'px';	
		this.element.show();
	},
	hide: function(element)
	{
		element = element == null ? 'preloader' : element;
		this.element = $(element);
		if (this.element == null) return;
		
		this.element.hide();
		this.element.style.left = '0px';
		this.element.style.top = '0px';
	},
	is_enable: function()
	{
		return (document.getElementById('preloader') != null);
	}
};

/*MonteCarloAjaxCnt = {activeRequestCount:0};
Ajax.Responders.register({
    onCreate: function(ajax) {
    	if(ajax.options.invisible) return; 
    
    	MonteCarloAjaxCnt.activeRequestCount++;
    	if(MonteCarloAjaxCnt.activeRequestCount == 1)
    	{
    		Preloader.show();
      }
    },
    onComplete: function(ajax) {
    	if(ajax.options.invisible) return;
    
    	MonteCarloAjaxCnt.activeRequestCount--;
      if(MonteCarloAjaxCnt.activeRequestCount == 0)
    	{
        Preloader.hide();
      }
    }
});*/
